/*
 * Js模块文件的入口
 * by hyi
 */
(function ($) {
  var btnl = $(".btn-l");

  var left = $(".left");
  var right = $(".right");
  var effect = $(".effect");
  var lines = $(".lines");
  var isSrc = false,
    isHide = false,
    hasChange = false;
  var editor = null;

  function changeSize() {
    $(".container").css("min-height", $(window).height() - 1 + "px");
    $(".code").css("height", $(window).height() + "px");
    $(".allexamples").css({ "min-height": $(window).height() + "px" });
    $(".all-inner").css("width", $(window).width() - 250 + "px");
    $(".right").css("width", $(window).width() - 250 + "px");
  }

  function hasChanged() {
    editor.on("change", function () {
      hasChange = true;
    });
  }

  function changeInnerSize() {
    if (isHide) {
      $(".all-inner").css("width", $(window).width() - 250 + "px");
      isHide = false;
    } else {
      $(".all-inner").css("width", $(window).width() + "px");
      isHide = true;
    }
  }

  btnl.click(function () {
    $(this).toggleClass("btn-l-r");
    left.toggleClass("l-trf-r");
    right.toggleClass("r-trf-l");
    lines.toggleClass("lines-arrow");
    if (isHide) {
      setTimeout(function () {
        $(".right").width($(window).width() - 250);
      }, 500);
    } else {
      $(".right").width($(window).width());
    }
    changeInnerSize();
  });

  changeSize();
  $(window).resize(function () {
    changeSize();
  });

  var examples = $(".allexamples .item p");
  examples.click(function (e) {
    var url = $(this).prev().attr("href");
    location.href = url;
  });

  $(".allexamples .item").hover(
    function (event) {
      $(this).find("p").stop(true, true);
      $(this).find("p").css("display", "block").animate({ opacity: 0.7 }, 200);
    },
    function () {
      $(this).find("p").stop(true, true);
      $(this).find("p").animate({ opacity: 0 }, 200).css("display", "none");
    }
  );

  var url = location.hash.substr(1) || "page/home.html";
  $("#iframes").attr("src", url);

  window.onhashchange = function () {
    var url = location.hash.substr(1);
    $("#iframes").attr("src", url);

    $(".nav a").removeClass("current");
    var item = $('.nav a[href="#' + url + '"]');
    item.addClass("current");
  };

  /*    var data = config.demos;

        var html = [];
        for (var i = 0; i < data.length; i++) {
            var item = data[i];
            html.push('<li><a class="index" href="#' + item.url + '" style="background-image:url(pics/' + item.pic + ')">' + item.title + '</a></li>');

        }
        $('.nav ul').html(html.join(''));

        $('.nav a').removeClass('current');
        var item = $('.nav a[href="#' + url + '"]');
        item.addClass('current');
        var scrollTop = item.position().top;
        $('.nav').scrollTop(scrollTop);*/

  var setting = {
    callback: {
      onClick: zTreeOnClick,
    },
  };

  function zTreeOnClick(event, treeId, treeNode) {
    if (!!treeNode.data_url) $("#iframes").attr("src", treeNode.data_url);
  }
  var zNodes = [
    {
      name: "地图",
      icon: "pics/ztree/root.gif",
      data_url: "1_map_init.html",
      open: true,
      children: [
        {
          name: "1_map_init",
          icon: "pics/ztree/file.gif",
          data_url: "1_map_init.html",
        },
        {
          name: "2_setCenter_setZoom",
          icon: "pics/ztree/file.gif",
          data_url: "2_setCenter_setZoom.html",
        },
        {
          name: "3_add_remove_marker",
          icon: "pics/ztree/file.gif",
          data_url: "3_add_remove_marker.html",
        },
        {
          name: "4_add_remove_line",
          icon: "pics/ztree/file.gif",
          data_url: "4_add_remove_line.html",
        },
        {
          name: "5_add_remove_polygon",
          icon: "pics/ztree/file.gif",
          data_url: "5_add_remove_polygon.html",
        },
        {
          name: "6_add_remove_circle",
          icon: "pics/ztree/file.gif",
          data_url: "6_add_remove_circle.html",
        },
        {
          name: "7_add_remove_rectangle",
          icon: "pics/ztree/file.gif",
          data_url: "7_add_remove_rectangle.html",
        },
        {
          name: "8_compute_distance_area",
          icon: "pics/ztree/file.gif",
          data_url: "8_compute_distance_area.html",
        },
        {
          name: "9_keyword_nearby_search",
          icon: "pics/ztree/file.gif",
          data_url: "9_keyword_nearby_search.html",
        },
        {
          name: "10_geocoder_test",
          icon: "pics/ztree/file.gif",
          data_url: "10_geocoder_test.html",
        },
        {
          name: "11_router_test",
          icon: "pics/ztree/file.gif",
          data_url: "11_router_test.html",
        },
        {
          name: "12_coordinate_test",
          icon: "pics/ztree/file.gif",
          data_url: "12_coordinate_test.html",
        },
        {
          name: "13_path_animation",
          icon: "pics/ztree/file.gif",
          data_url: "13_path_animation.html",
        },
        {
          name: "14_add_markercluster",
          icon: "pics/ztree/file.gif",
          data_url: "14_add_markercluster.html",
        },
        {
          name: "15_info_window",
          icon: "pics/ztree/file.gif",
          data_url: "15_info_window.html",
        },
        {
          name: "16_set_bounds",
          icon: "pics/ztree/file.gif",
          data_url: "16_set_bounds.html",
        },
        {
          name: "17_on_zoom_change",
          icon: "pics/ztree/file.gif",
          data_url: "17_on_zoom_change.html",
        },
      ],
    },

    {
      name: "控件",
      icon: "pics/ztree/folder.gif",
      children: [
        {
          name: "logo",
          icon: "pics/ztree/file.gif",
          data_url: "",
        },
        {
          name: "Zoom缩放",
          icon: "pics/ztree/file.gif",
          data_url: "",
        },
        {
          name: "Scale比例尺",
          icon: "pics/ztree/file.gif",
          data_url: "",
        },
        {
          name: "Fullscreen比例尺",
          icon: "pics/ztree/file.gif",
          data_url: "",
        },
        {
          name: "ExportImage导出图片",
          icon: "pics/ztree/file.gif",
          data_url: "",
        },
        {
          name: "MapTheme底图管理",
          icon: "pics/ztree/file.gif",
          data_url: "",
        },
        {
          name: "LayerSwitch图层管理",
          icon: "pics/ztree/file.gif",
          data_url: "",
        },
      ],
    },
    {
      name: "数据服务",
      icon: "pics/ztree/book.gif",
      children: [
        {
          name: "道路查询",
          icon: "pics/ztree/file.gif",
          data_url: "",
        },
      ],
    },
  ];
  $.fn.zTree.init($("#treeDemo"), setting, zNodes);
})(jQuery);
